﻿using RLRT.Common;
using RLRT.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace RLRT.Web
{
    public partial class SelectBOM : LocalDialog
    {
        #region 属性

        /// <summary>
        /// 工令号
        /// </summary>
        private string CurrentOrNo
        {
            get
            {
                object tempObject = ViewState["CurrentOrNo"];
                return (tempObject != null) ? (string)tempObject : "";
            }
            set
            {
                ViewState["CurrentOrNo"] = value;
            }
        }

        #endregion

        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region 验证权限

                #endregion

                #region 获取参数

                string orNo = Request.QueryString["OrNo"];
                if (orNo != null && orNo.Trim().Length != 0)
                {
                    this.CurrentOrNo = orNo.Trim();
                }

                #endregion

                //初始化
                this.Initialize();
            }

            #region 页面标题

            this.Title = PageTitle.Text;

            #endregion

            #region 页面要素

            //this.AssignDialogCallback(CancelButton, "'Close'", "window");

            #endregion
        }

        #region 初始化

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            #region 页面内容

            #endregion

            this.BindList();
        }

        #endregion

        #region 绑定数据

        private void BindList()
        {

            using (RTProvider rtProvider = new RTProvider())
            {
                List.DataSource = rtProvider.GetBOMDetailList(this.CurrentOrNo);
            }

            List.DataBind();
        }

        protected void List_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            List.PageIndex = e.NewPageIndex;
            this.BindList();
        }

        #endregion

        #region 操作

        protected void OkButton_Click(object sender, EventArgs e)
        {
            var table = LocalGlobal.tmpTable;

            bool flag = false;
            foreach (GridViewRow row in List.Rows)
            {
                HtmlInputCheckBox rowCheckControl = (HtmlInputCheckBox)row.FindControl("RowCheck");
                if (rowCheckControl.Checked)
                {
                    var newRow = table.NewRow();
                    newRow["bomseq"] = row.Cells[1].Text.Trim();
                    newRow["bompno"] = row.Cells[2].Text.Trim();
                    newRow["bomnam"] = row.Cells[3].Text.Trim();
                    newRow["bomspc"] = row.Cells[4].Text.Trim();
                    newRow["bommat"] = row.Cells[5].Text.Trim();
                    newRow["bomuit"] = row.Cells[6].Text.Trim();
                    newRow["bomreq"] = row.Cells[7].Text.Trim();
                    newRow["pickqty"] = row.Cells[7].Text.Trim();

                    //保存临时数据后重新绑定gridview
                    table.Rows.Add(newRow);

                    flag = true;
                }
            }

            if (!flag)
            {
                this.ShowInfoMessage(this.GetGlobalResourceString("NotSelectMessage"));
                return;
            }

            //回调
            this.DialogCallback("'OK'", "{host:window}");
        }

        #endregion
    }
}